<template>
  <div>
    <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
      <el-breadcrumb-item :to="{ path: '/ManageHome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>内容管理</el-breadcrumb-item>
      <el-breadcrumb-item>景点信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
      <div>
        <!--    功能区-->
        <div style="margin: 10px 0">
          <!--    搜索区-->
          <div style="margin: 10px 0">
            <el-input v-model="search" clearable placeholder="请输入姓名" prefix-icon="el-icon-search" style="width: 20%" />
            <el-button icon="el-icon-search" @click="load"
              style="background-color: #143371; color: aliceblue;">搜索</el-button>
            <el-button icon="el-icon-refresh-left" style="margin-left: 5px;background-color: #143371; color: aliceblue;"
              @click="reset">重置</el-button>
            <div style="float: right">

              <el-tooltip content="添加学生" placement="top">

                <el-button style="background-color: #143371; color: aliceblue;width: 40px;" icon="el-icon-plus"
                  @click="add"></el-button>
              </el-tooltip>
            </div>
          </div>
        </div>
        <!--    表格-->
        <el-table v-loading="loading" :data="tableData" max-height="1000" style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="景点名称">
                  <span>{{ props.row.scName }}</span>
                </el-form-item>
                <el-form-item label="景点概要">
                  <p>{{ props.row.scDesc }}</p>
                </el-form-item>
                <el-form-item label="景点封面">
                  <img :src="require('../assets/scenery/' + props.row.scImage)" alt="" style="width: 180px; height: 150px;">
                </el-form-item>
                <el-form-item label="更多图片">
                  <div style="display: flex;">
                    <img :src="require('../assets/scenery/' + item)" alt="" v-for="(item, index) in props.row.scImages"
                      :key="index" style="width: 180px; height: 150px;">
                  </div>
                </el-form-item>
                <el-form-item label="景点介绍">
                  <div v-html="props.row.scDetail"></div>
                </el-form-item>


              </el-form>
            </template>
          </el-table-column>


          <el-table-column label="#" type="index" />
          <el-table-column label="景点编号" prop="scId" sortable width="100" />
          <el-table-column label="名称" prop="scName" width="180px" />
          <el-table-column label="概要" prop="scDesc" width="180px">
          </el-table-column>
          <el-table-column label="地址" prop="scAdress" width="150px" />
          <el-table-column label="开放时间" prop="scTime" />
          <el-table-column label="服务电话" prop="scPhone" width="120px" />
          <el-table-column label="起价" width="120px">
            <template slot-scope="scope">
              <span>{{ scope.row.scPrice != 0 ? scope.row.scPrice : 免费 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="开放状态">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.scStatus" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>


          <!--      操作栏-->
          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div style="margin: 10px 0">
          <el-pagination :currentPage="currentPage" :page-size="pageSize" :page-sizes="[10, 20]" :total="total"
            layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
          </el-pagination>
        </div>
        <div>
          <!--      弹窗-->
          <el-dialog :visible.sync="dialogVisible" title="操作" width="50%" @close="cancel" top="30px">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">

              <el-form-item label="名称" prop="scName">
                <el-input v-model="form.scName" style="width: 80%" placeholder="景点的名字"></el-input>
              </el-form-item>

              <el-form-item label="概述" prop="scDesc">
                <el-input v-model="form.scDesc" style="width: 80%" placeholder="用一句话描述它"></el-input>
              </el-form-item>

              <el-form-item label="地址" prop="scAdress">
                <el-input v-model="form.scAdress" style="width: 80%" placeholder="在此输入地址"></el-input>
              </el-form-item>

              <el-form-item label="服务电话" prop="scPhone">
                <el-input v-model.number="form.scPhone" style="width: 80%" placeholder="在此填写服务电话/咨询电话"></el-input>
              </el-form-item>

              <el-form-item label="开放时间" prop="scTime">
                <el-input v-model="form.scTime" style="width: 80%" placeholder="格式为 HH:MM - HH:MM"></el-input>
              </el-form-item>

              <el-form-item label="起价" prop="scPrice">
                <el-input v-model.number="form.scPrice" style="width: 80%" placeholder="免费请填0,否则填起价（最低价）"></el-input>
              </el-form-item>

              <el-form-item label="封面图" prop="scImage">
                <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :limit="1"
                  :on-preview="handlePictureCoverPreview" :on-remove="handleCoverRemove" :on-success="handleCover" accept=".jpg,.png"
                  :on-exceed="handleExceed"
                  >
                  <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="CoverdialogVisible">
                  <img width="100%" :src="CoverdialogImageUrl" alt="">
                </el-dialog>
             
              </el-form-item>
              <el-form-item label="景点图片(最多9张)" prop="scImages">
                <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :limit="9"
                  :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :on-success="handleMoreImages" 
                  accept=".jpg,.png">
                  <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="MoredialogVisible">
                  <img width="100%" :src="MoredialogImageUrl" alt="">
                </el-dialog>
             
              </el-form-item>
              <el-form-item label="景点详细介绍" prop="scDetail">
                <el-input
                    type="textarea"
                    autosize
                    placeholder="请输入内容"
                    v-model="form.scDetail">
                  </el-input>
              </el-form-item>

            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
              </span>
            </template>
          </el-dialog>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import request from '@/utils/request'
export default {
  name: "SceneryInfo",
  data() {
    // 手机号验证
    const checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|6|7|8][0-9]{9}$/;
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(value)) {
            callback();
          } else {
            callback(new Error("电话号码格式不正确"));
          }
        }
      }, 100);
    };

    return {
      loading: true,
      editJudge: true,
      judge: false,
      dialogVisible: false,
      search: "",
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [
        {
          scId: 1,
          scName: '张家界森林公园',
          scDesc: '自在悠然的美景',
          scAdress: '永定区',
          scPhone: '13456567786',
          scDetail: '<h4>111111111<h4>',
          scPrice: 100,
          scImages: ['lhs3.jpg', 'ls1.jpg', 'sqs6.jpg'],
          scImage: 'lhs4.jpg',
          scStatus: true
        }
      ],
      form: {

        //scId:1,
        scName: '张家界森林公园',
        scDesc: '自在悠然的美景',
        scAdress: '永定区',
        scPhone: '13456567786',
        scDetail: '<h4>111111111<h4>',
        scPrice: 100,
        scImages: ['lhs3.jpg', 'ls1.jpg', 'sqs6.jpg'],
        scImage: 'lhs4.jpg',
        //scStatus:true

      },
      rules: {
        stuId: [
          { required: true, message: "请输入学号", trigger: "blur" },
        ],
        stuName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
        ],
        stuGrade: [
          { required: true, message: "请输入年级", trigger: "blur" },
          { type: "number", message: "年级必须为数字值", trigger: "blur" },
        ],
        stuSex: [{ required: true, message: "请选择性别", trigger: "change" }],
        stuPhone: [{ required: true, validator: checkPhone, trigger: "blur" }],
        stuSfid: [
          { required: true, message: "请输入身份证", trigger: "blur" },
        ],
        ldId: [{ required: true, message: "请输入楼栋id", trigger: "blur" }],
        roomId: [{ required: true, message: "请选择性别", trigger: "change" }]
      },
      editDisplay: {
        display: "block",
      },
      display: {
        display: "none",
      },
    };
  },
  created() {
    this.load();
    this.loading = true;
    setTimeout(() => {
      //设置延迟执行
      this.loading = false;
    }, 1000);
  },
  methods: {

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleCover(res, file, fileList) {

    },
    handleMoreImages(res, file, fileList){

    },
    handleExceed(){
      this.$message.warning(`只允许上传一张图片`);
    },
    async load() {
      // request.get("/student/find", {
      //       params: {
      //           pageNum: this.currentPage,
      //           pageSize: this.pageSize,
      //           search: this.search,
      //       },
      //   }).then((res) => {
      //       console.log(res);
      //       this.tableData = res.data.records;
      //       this.total = res.data.total;
      //       this.loading = false;
      //   });
    },
    reset() {
      // this.search = ''
      // request.get("/student/find", {
      //     params: {
      //         pageNum: 1,
      //         pageSize: this.pageSize,
      //         search: this.search,
      //     },
      // }).then((res) => {
      //     console.log(res);
      //     this.tableData = res.data.records;
      //     this.total = res.data.total;
      //     this.loading = false;
      // });
    },
    handleDelete(index, row) {
      // 确认框确认是否要删除
      this.$confirm("确定要删除" + row.stuName + "的信息吗?", "删除提示", {
        iconClass: "el-icon-question", //自定义图标样式
        confirmButtonText: "残忍删除", //确认按钮文字
        cancelButtonText: "取消删除", //取消按钮文字
        showClose: true, //是否显示右上角关闭按钮
        type: "warning", //提示类型  success/info/warning/error
      }).then(() => {  //选择确认按钮进入此方法
        request.delete("student/delete/" + row.stuId).then(res => {
          console.log(row.stuId)
          if (res.code === "1") {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.load()
          }
          else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
        })
      }).catch(() => { //选择取消按钮进入此方法
        //取消操作
        alert('取消')
      });
    },
    handleEdit(index, row) {
      this.judge = true;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        this.form = JSON.parse(JSON.stringify(row));
        this.editDisplay = { display: "block" };
      });
    },
    cancel() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    save() {
      this.$refs.form.validate(vaild => {
        if (vaild) {
          if (this.judge) {
            //先表单验证，再放put请求
            request.put("/student/update", this.form).then(res => {
              if (res.code === "1") {
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
                this.search = "",
                  this.load();
                this.dialogVisible = false
              }
              else {
                this.$message({
                  type: 'error',
                  message: res.msg
                })
              }
            })


          }
          else {
            //发post请求
            console.log(1)
            request.post("/student/add", this.form).then(res => {
              if (res.code === "1") {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.search = "",
                  this.load();
                this.dialogVisible = false
              }
              else {
                this.$message({
                  type: 'error',
                  message: res.msg
                })
              }
            })
          }
        }
      })


    },
    add() {
      this.dialogVisible = true;
      this.judge = false;
      this.form = {}
    },
    handleSizeChange(pageSize) {
      //改变每页个数
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      //改变页码
      this.currentPage = pageNum;
      this.load();
    },

  }


}
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 80%;
  display: block;
}
</style>